package com.example.Words.room.util;

import android.content.Context;

import androidx.annotation.NonNull;
import androidx.room.Database;
import androidx.room.Room;
import androidx.room.RoomDatabase;
import androidx.room.migration.Migration;
import androidx.sqlite.db.SupportSQLiteDatabase;

import com.example.Words.room.model.Dao.WordDao;
import com.example.Words.room.model.entity.Word;

/**
 * <pre>
 *  author:huletian
 *  time    :2020/2/22
 *  desc    :
 *  version:1.0
 * </pre>
 */

@Database(entities = Word.class,version = 4,exportSchema = false)
public abstract class WordDatabaseUtil extends RoomDatabase {
    private static WordDatabaseUtil INSTANCE;
    public   static WordDatabaseUtil getDatabase(Context context){
        if(INSTANCE==null){
            INSTANCE= Room.databaseBuilder(context.getApplicationContext(),WordDatabaseUtil.class,"word_database")
                   .addMigrations(MIGRATION_3_4)
                   // .fallbackToDestructiveMigration()
                    .build();
        }
        return INSTANCE;
    }
    public abstract WordDao getWordDao();
    static final Migration MIGRATION_2_3=new Migration(2,3) {
        @Override
        public void migrate(@NonNull SupportSQLiteDatabase database) {
            database.execSQL("alter table word add column foo_data text ");
        }
    };
    static final Migration MIGRATION_3_4=new Migration(3,4) {
        @Override
        public void migrate(@NonNull SupportSQLiteDatabase database) {
            database.execSQL("alter table word add column chinese_invisible integer not null default 0");
        }
    };
}
